﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using MySite.Model;
using MySite.BLL;

namespace MySite.Controllers
{
    /// <summary>
    /// 用于记录当前登陆用户的相关数据
    /// </summary>
    public class CurrentHelper
    {
        //用于记录当前用户帐号信息
        /// <summary>
        /// 用于记录当前用户帐号信息
        /// </summary>
        private static AccountInfo m_CurrentUser;

        /// <summary>
        /// 是否已经记录当前访问者
        /// </summary>
        private static bool m_IsRecordVisit;

        /// <summary>
        /// 当前登陆的用户
        /// </summary>
        public static AccountInfo CurrentUser
        {
            get
            {
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session["User"] != null)
                {
                    m_CurrentUser = (AccountInfo)System.Web.HttpContext.Current.Session["User"];
                }
                else if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request.IsAuthenticated && !string.IsNullOrEmpty(System.Web.HttpContext.Current.User.Identity.Name))
                {
                    m_CurrentUser = new Account().Query(string.Format("Account_Name='{0}'", System.Web.HttpContext.Current.User.Identity.Name)).FirstOrDefault();
                    System.Web.HttpContext.Current.Session["User"] = m_CurrentUser;
                }
                return m_CurrentUser;
            }
            set
            {
                m_CurrentUser = value;
                if (System.Web.HttpContext.Current != null)
                {
                    System.Web.HttpContext.Current.Session["User"] = m_CurrentUser;
                }
                
            }
        }

        /// <summary>
        /// 当前登陆的用户
        /// </summary>
        public static bool IsRecordVisit
        {
            get
            {
                if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Session["IsRecordVisit"] != null)
                {
                    m_IsRecordVisit = Convert.ToBoolean(System.Web.HttpContext.Current.Session["IsRecordVisit"]);
                }
                return m_IsRecordVisit;
            }
            set
            {
                m_IsRecordVisit = value;
                if (System.Web.HttpContext.Current != null)
                {
                    System.Web.HttpContext.Current.Session["IsRecordVisit"] = m_IsRecordVisit;
                }

            }
        }
    }
}
